package com.angel.web.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.Constants;
import com.angel.core.base.JsonEntity;
import com.angel.core.util.JsonUtil;
import com.angel.core.util.MD5Util;
import com.angel.core.util.SMSUtils;
import com.angel.health.SMS;
import com.angel.health.SysSendMessage;
import com.angel.health.SysUser;
import com.angel.health.SysUserExt;
import com.angel.provider.sys.SysSendMessageProvider;
import com.angel.provider.sys.SysUserProvider;

/**
 * @author cheng sir
 * 2016年9月14日上午10:36:40
 */

@Controller
@RequestMapping("/sysUser")
public class SysUserController {

	@Autowired
	private SysUserProvider provider;
	@Autowired
	private SysSendMessageProvider sysSendMessageProvider;
	
	//访问登陆页
	@RequestMapping("/index")
	public String index(){
		return "login";
	}
	
	//访问首页
	@RequestMapping("/success")
	public String success(){
		return "index";
	}
	
	//跳转到修改密码界面
	@RequestMapping("/repassword")
	public String repassword(){
		return "editPassword";
	}
	
	//登录
	@RequestMapping("/login")
	public String login(SysUser user,HttpServletRequest request){
		user.setLoginIp(request.getRemoteAddr());
		Map<String, Object> map = provider.login(user);
		request.setAttribute("map", map);
		if("2".equals(map.get("code"))){
			SysUser sysUser =  (SysUser) map.get("msg");
			request.getSession().setAttribute("user", sysUser.getLoginName());
			request.getSession().setAttribute("currentUser", sysUser);
			return "index";
		}else{
			request.setAttribute("name", user.getLoginName());
			request.setAttribute("pwd", user.getPassword());
			return "login";
		}
	}
	
	//忘记密码
	@RequestMapping("/forgetPwd")
	public String forgetPwd(){
		return "accountValidate";
	}
	
	//用户验证
	@RequestMapping("/accountValidate")
	public String accountValidate(SysUser user,String certPic,HttpServletRequest request){
		String validatecode = (String) request.getSession().getAttribute("key");
		Map<String, Object> map = provider.accountValidate(user,certPic,validatecode);
		request.setAttribute("map", map);
		if("2".equals(map.get("code"))){
			SysUser sysUser = (SysUser) map.get("sysUser");
			request.getSession().setAttribute("sysUser", sysUser);
			return "phoneValidate";
		}else{
			
			return "accountValidate";
		}
	}
	
	//发送手机验证码
	@ResponseBody
	@RequestMapping(value="getCode", produces = "application/json; charset=utf-8")
	public String getCode(String memPhone,String type){
		if(StringUtils.isEmpty(memPhone)){
			return JsonUtil.toJson(new JsonEntity("-1","手机号不存在！"));
		}
		String content = "为您在天使健康的找回密码验证码【天使健康】";
		SysUser user = new SysUser();
		user.setMobile(memPhone);
		List<Map<String,Object>> list = provider.findAll(user);
		if("1".equals(type)){
			content = "为您在天使健康的注册验证码【天使健康】";
			if(list.size() > 0){
				return JsonUtil.toJson(new JsonEntity("-1","已注册"));
			}
		}else{
			if(list.size() == 0){
				return JsonUtil.toJson(new JsonEntity("-1","未注册"));
			}
		}
		String code = "";
		Random random = new Random();
		for(int i = 0;i<4;i++){
			int nextInt = random.nextInt(10);
			code += nextInt;
		}
		content = code+content;
		SysSendMessage sysSendMessage = new SysSendMessage(1,memPhone,content,null);
		int insert = sysSendMessageProvider.insert(sysSendMessage);
		if(insert > 0){
			String single = Constants.SEND_SMS_SINGLE;
			JsonEntity json = SMSUtils.send(new SMS(sysSendMessage.getId().toString(),memPhone,content,sysSendMessage.getId().toString(),""),single);
			if("200".equals(json.getCode())){
				return JsonUtil.toJson(new JsonEntity("200",code));
			}
			return JsonUtil.toJson(json);
		}
		return JsonUtil.toJson(new JsonEntity("-1","发送失败！"));
	}
	
	//手机验证
	@RequestMapping("/phoneValidate")
	public String phoneValidate(SysUser user,String certPic,String sendCertPic,HttpServletRequest request){
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		Map<String, Object> map = provider.phoneValidate(user,certPic,sendCertPic,sysUser);
		request.setAttribute("map", map);
		if("2".equals(map.get("code"))){
			return "resetValidate";
		}else{
			return "phoneValidate";
		}
	}
	
	//重置密码
	@RequestMapping("/resetValidate")
	public String resetValidate(SysUser user,String repassword,HttpServletRequest request){
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		Map<String, Object> map = provider.resetValidate(user,sysUser,repassword);
		request.setAttribute("map", map);
		if("2".equals(map.get("code"))){
			return "successValidate";
		}else{
			return "resetValidate";
		}
	}
	
	//修改密码
	@RequestMapping("/resetPassword")
	@ResponseBody
	public String resetPassword(SysUser user,String newPassword,String repassword,HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		SysUser sysUser = (SysUser) request.getSession().getAttribute("currentUser");
		//判断是否直接get方式访问
		if(sysUser == null){
			map.put("msg", "用户非正常访问");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		//判断新密码是否为空
		if(user == null || user.getPassword() == null || user.getPassword() == ""){
			map.put("msg", "请输入旧密码");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		if(!MD5Util.getMD5(user.getPassword()).equals(sysUser.getPassword())){
			map.put("msg", "旧密码输入不正确");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		if(StringUtils.isBlank(newPassword)){
			map.put("msg", "新密码不能为空");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		if(!newPassword.equals(repassword)){
			map.put("msg", "两次密码输入不一致");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		sysUser.setPassword(MD5Util.getMD5(newPassword));
		provider.resetpassword(sysUser);
		request.getSession().invalidate();
		map.put("msg", "更换密码成功");
		map.put("code", "1");
		return JsonUtil.toJson(map);
	}
	//查看个人信息
	@RequestMapping("/findUserInfo")
	public String findUserInfo(SysUser user,HttpServletRequest request){
		SysUser sysUser = (SysUser) request.getSession().getAttribute("currentUser");
		SysUserExt ext = provider.findUserInfo(sysUser);
		request.setAttribute("ext", ext);
		return "lookUserInfo";
	}
	
	//退出登录
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request){
		request.getSession().invalidate();
		return "login";
	}
}
